package fr.channel.server.metier;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import fr.channel.server.PMF;
import fr.channel.server.model.Joueur;
import fr.channel.server.model.Partie;

public class SbChannel {

	PersistenceManager pm = PMF.get().getPersistenceManager();

	public void save(Object object) {

		try {
			pm.makePersistent(object);
		} finally {
			pm.close();
		}
		pm = PMF.get().getPersistenceManager();
	}

	@SuppressWarnings("unchecked")
	public final List<Partie> getAllParties() {
		Query query = pm.newQuery(Partie.class);
		List<Partie> result = (List<Partie>) query.execute();
		query.closeAll();
		return result;
	}

	@SuppressWarnings("unchecked")
	public final Partie findPartieByName(final String name) {
		Query query = pm.newQuery(Partie.class);
		query.setFilter("name == nameParam");
		query.declareParameters("String nameParam");
		List<Partie> parties = (List<Partie>) query.execute(name);
		query.closeAll();
		if (parties.isEmpty()) {
			return null;
		}
		return parties.get(0);
	}

	@SuppressWarnings("unchecked")
	public final List<Joueur> getAllJoueurs() {
		Query query = pm.newQuery(Joueur.class);
		List<Joueur> result = (List<Joueur>) query.execute();
		query.closeAll();
		return result;
	}
}
